﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.信号处理模块
{
    public  class DACBase
    {
        public int iopen = 0;//0表示未开始，1表示不正常，2表示正常
        public string sxhbmc = "";
        public bool bdqxhb = false;
        //public mainform m_mainform;
       public float[] fAIDataex=new float[5] { 0, 0, 0, 0, 0};
       public float[] fTIData = new float[2] { 0, 0 };
       public bool[] bDIData = new bool[8] { false, false, false, false, false, false, false, false };
       public float[] fAIDataexnew = new float[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
       public float[] fAIDataZeroduoshao = new float[5] { 0, 0, 0, 0, 0 };

       public bool[] bDIDataJDQ = new bool[8] { false, false, false, false, false, false, false, false };
       public float[] fWLData = new float[2] { 0, 0 };
        public SerialPort commex = new SerialPort();//信号板

        public virtual bool openCOM(string sSet1)
        {
            string[] szbzlCOM = new string[5];
            if (sSet1.Length > 0)
                szbzlCOM = sSet1.Split(':');
            //打开串口
            commex.PortName = szbzlCOM[0];
            commex.BaudRate = Convert.ToInt32(szbzlCOM[1]);

            commex.NewLine = "/r/n";
            commex.RtsEnable = true;//根据实际情况吧。 
            commex.ReadTimeout = 300;//读取数据延时，避免读取不全数据

            if (szbzlCOM[3] == "0")
                commex.Parity = Parity.None;
            else
                commex.Parity = Parity.Even;
            // ////添加事件注册  
            //  comm.DataReceived += comm_Data;
            try
            {
                commex.Open();
            }
            catch (Exception )
            {
                //lzj20200706 MessageBox.Show(string.Format("{0}\n请检查电脑的串口故障", ex.Message));
                return false;
            }
            return true;
        }

        public virtual bool SendDeviceCmd(int cmd)
        {
            return true;
        }

        public void PutVol(double fOutPut)//模块电流输出
        {
            fOutPut = Math.Round(fOutPut);
            short usData;
            if (fOutPut < 0)
                fOutPut = 0;
            else if (fOutPut > 4095)
                fOutPut = 4095;

            baocunriji("输出", string.Format("{0}", fOutPut));
            int ioutput = Convert.ToInt32(fOutPut);

            usData = short.Parse(string.Format("{0}", ioutput));
            short usShift = usData;
            byte[] pData = new byte[3] { 0xFF, 0xFF, 0xFF };
            pData[0] = byte.Parse(string.Format("{0}", (usShift / 0x100)));
            usShift %= 0x100;
            pData[1] = byte.Parse(string.Format("{0}", (usShift / 0x10))); //(BYTE)(usShift / 0x10);
            usShift %= 0x10;
            pData[2] = byte.Parse(string.Format("{0}", usShift)); //(BYTE)usShift;
            pData[0] &= 0x0F;
            pData[0] |= 0x30;
            pData[1] &= 0x0F;
            pData[1] |= 0xC0;
            pData[2] &= 0x0F;
            pData[2] |= 0xA0;
            if (commex.IsOpen)
            {
                //this.comDA.DiscardInBuffer();
                Byte[] m_byte = new Byte[3];
                for (int i = 0; i < 3; i++)
                {
                    m_byte[i] = pData[i];
                }
                commex.Write(m_byte, 0, 3);
            }
        }

        public void SetDOBit(short m_uChannel, short uData)//数字位输出
        {

            byte[] cData = new byte[6] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
            cData[0] = 0xFF;//开始
            cData[1] = byte.Parse(string.Format("{0}", m_uChannel + 1));//通道位
            cData[2] = byte.Parse(string.Format("{0}", uData << m_uChannel));//状态位
            cData[3] = 0xFE;//结束
            short uSUM = short.Parse(string.Format("{0}", cData[0] + cData[1] + cData[2] + cData[3]));
            cData[5] = byte.Parse(string.Format("{0}", uSUM / 156));
            cData[4] = byte.Parse(string.Format("{0}", uSUM % 156));
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[6];
                for (int i = 0; i < 6; i++)
                {
                    m_byte[i] = cData[i];
                }
                commex.Write(m_byte, 0, 6);
            }

        }

        public void baocunriji(string sname, string sneirong) //
        {
            string syy = DateTime.Now.ToString("yyyy");
            string smm = DateTime.Now.ToString("MM");
            string sdd = DateTime.Now.ToString("dd");
            string smul = string.Format("{0}\\{1}\\{2}\\{3}\\{4}", System.Windows.Forms.Application.StartupPath, "日志信息", syy, smm, sdd);
            if (!Directory.Exists(smul))
            {
                Directory.CreateDirectory(smul);
            }
            string path = "";
            path = string.Format("{0}\\{1}.ini", smul, sname);
            iniclass objini = new iniclass();
            objini.Ini(path);
            objini.Writue("日记", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), sneirong);
        }
        public void close()
        {
            //关闭串口
            if (commex.IsOpen)
            {
                commex.Close();
            }
        }

        ~DACBase()//
        {
            //关闭串口
            if (commex.IsOpen)
            {
                commex.Close();
            }
        }

        public void Write(string path, string str)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write((str).ToString());
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public void SaveLog(string fileName, string jkid, string message)
        {
            StringBuilder path = new StringBuilder();
            StringBuilder pathRoot = new StringBuilder();
            try
            {
                pathRoot.Append(AppDomain.CurrentDomain.BaseDirectory);
                pathRoot.Append("\\log" + "\\");
                pathRoot.Append(DateTime.Now.ToString("yyyyMMdd"));
                if (!Directory.Exists(pathRoot.ToString()))
                {
                    Directory.CreateDirectory(pathRoot.ToString());
                }
                path.Append(pathRoot.ToString() + "\\");
                path.Append(fileName);
                path.Append(@".log");
                if (File.Exists(path.ToString()))
                {
                    StreamWriter sr = File.AppendText(path.ToString());
                    sr.WriteLine(DateTime.Now.ToString() + "【" + jkid + "】" + message);
                    sr.Close();
                }
                else
                {
                    StreamWriter sr = File.CreateText(path.ToString());
                    sr.WriteLine(DateTime.Now.ToString() + "【" + jkid + "】" + message);
                    sr.Close();
                }

            }
            catch (Exception)
            {

            }

        }
    }
}
